package com.tungse.proxy_dome;

import java.lang.reflect.Proxy;
import java.util.Arrays;

/**
 * @Author : Tungse
 * @Date: 2025/8/22 15:46
 * @Description:
 */
public class DynamicProxyTest {
    public static void main(String[] args) {
        // 创建实际对象
        UserService realService = new UserServiceImpl();
        StringBuilder logArea = new StringBuilder();
        // 创建动态代理
        UserService userServiceProxy = (UserService) Proxy.newProxyInstance(
            UserService.class.getClassLoader(),
            new Class<?>[]{UserService.class},
            new LoggingHandler(realService, logArea)
        );

        logArea.append("动态代理创建成功!\n");
        logArea.append("代理类: " + userServiceProxy.getClass().getName() + "\n");
        logArea.append("实现的接口: " + Arrays.toString(userServiceProxy.getClass().getInterfaces()) + "\n\n");

        userServiceProxy.addUser("张三");
        userServiceProxy.getUserInfo("张三");
    }
}
